package com.smartthinking.destinomusical;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.smartthinking.destinomusical.adapter.CatListAdapter;
import com.smartthinking.destinomusical.base.BaseActivity;
import com.smartthinking.destinomusical.base.ListBaseActivity;
import com.smartthinking.destinomusical.extra.Query;
import com.smartthinking.destinomusical.interfaces.WSInterface;
import com.smartthinking.destinomusical.obj.Categoria;
import com.smartthinking.destinomusical.obj.SearchParams;
import com.smartthinking.destinomusical.ws.WSCallTask;

public class Categorias extends ListBaseActivity implements WSInterface {

	WSCallTask hilo;
	List<Categoria> adapterList;
	CatListAdapter catListAdapter;
	boolean clear = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.categorias);

		setTopBar(0, 0, R.string.boton_categoria);
		hilo = new WSCallTask(this, this);
		hilo.execute(new SearchParams(1, Query.Categorias(null)));
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		Categoria obj = (Categoria) l.getItemAtPosition(position);
		Intent intent = new Intent();
		if (obj.getHijos().equals(null) || obj.getHijos().equals("")) {
			intent.setClass(getApplicationContext(), CancionPorCategoria.class);
		} else {
			intent.setClass(getApplicationContext(), SubCategorias.class);
		}
		intent.putExtra("padre", obj);
		intent.putExtra("usuario", usuario);
		startActivity(intent);

	}

	@SuppressWarnings("unchecked")
	@Override
	public void onWSInterfaceComplete(List<?> list) {

		if (list == null || list.size() == 0) {
			Toast.makeText(getApplicationContext(),
					getString(R.string.msg_noResults), Toast.LENGTH_SHORT)
					.show();
			clear = false;

		} else {
			adapterList = (List<Categoria>) list;
		    catListAdapter = new CatListAdapter(Categorias.this,
					list);
			getListView().setAdapter(catListAdapter);
			clear = true;
		}
	}

	@Override
	public void onWSInterfaceCancel(boolean cancelado) {
		// TODO Auto-generated method stub

	}

	public void onClickAll(View view) {
		Intent intent = new Intent(this, Explorar.class);
		intent.putExtra("usuario", usuario);
		startActivity(intent);

	}
	
	@Override
	public void onDestroy(){
		super.onDestroy();
		if(clear){
			catListAdapter.clearCache();
			
		}
	}

}